<template>
  <div class="loginContainer">
    <el-form
      :rules="rules"
      ref="loginForm"
      :model="loginForm"
      v-loading="loading"
      element-loading-text="正在登录..."
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0,0,0,0.8)"
      class="loginForm"
    >
      <h1 class="loginTitle">系统登录</h1>
      <el-form-item prop="username">
        <el-input
          type="text"
          auto-complete="false"
          v-model="loginForm.username"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          auto-complete="false"
          v-model="loginForm.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          v-model="loginForm.code"
          placeholder="点击图片刷新验证码"
          style="width: 170px; margin-right: 20px;"
        ></el-input>
        <img :src="capthchaUrl" @click="updateCapthcha" />
      </el-form-item>
      <el-checkbox v-model="checked">记住我</el-checkbox>
      <el-button type="primary" style="width:100%" @click.prevent="submitLogin"
        >登录</el-button
      >
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  //import引入的组件需要注入到对象中才能使用",
  components: {},
  data() {
    //这里存放数据",
    return {
      //验证码
      capthchaUrl: "/captcha?time=" + new Date(),
      //登陆信息
      loginForm: {
        username: "admin",
        password: "123",
        code: ""
      },
      //记住我
      checked: true,
      //element加载动画
      loading: false,
      //ele校验规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 16, message: "长度在 3 到 16 个字符", trigger: "blur" }
        ],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
      }
    };
  },
  mounted: function() {},
  methods: {
    //提交登录信息
    submitLogin() {
      this.$refs.loginForm.validate(data => {
        if (data) {
          this.loading = true;
          this.postRequest("/login", this.loginForm).then(resp => {
            if (resp) {
              this.loading = false;
              const tokenStr = resp.obj.tokenHead + resp.obj.token;
              //存储用户token
              window.sessionStorage.setItem("tokenStr", tokenStr);
              //  replace点击倒退键不会跳回登录页，push会跳回登录页
              // 跳转首页
              //从redirect获取目标地址，传入main.js的全局路由导航守卫=> to.path
              let path = this.$route.query.redirect;
              this.$router.replace(
                path === "/" || path == undefined ? "/home" : path
              );
            } else {
              this.loading = false;
            }
          });
          // this.$message({
          //   message: '用户登录',
          //   type: 'success'
          // });
        } else {
          this.loading = false;
          this.updateCapthcha();
          this.$message.error("请输入所有字段！");
          return false;
        }
      });
    },
    //刷新验证码
    updateCapthcha() {
      this.capthchaUrl = "/captcha?time=" + new Date();
    }
  }
};
</script>

<style>
.loginContainer {
  width: 100%;
  height: 100%;
  background-image: url(../static/img/20190414165045567.jpg);
  background-size: cover;
  background-position: center;
  position: fixed;
}

.loginForm {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 260px auto;
  width: 500px;
  padding: 15px 35px 15px 35px;
  background-color: rgba(255, 255, 255, 0.7);

  border: 1px solid rgb(87, 125, 139);
  box-shadow: 0 0 25px #cac6c6;
}

.loginTitle {
  margin: 0px auto 40px;
  text-align: center;
}

.el-form-item__content {
  display: flex;
  align-items: center;
}
</style>
